package com.hmdp.service.impl;

import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    private FollowMapper followMapper;
    @Override
    public Result handleFollow(Long id, Boolean isFollow) {
        UserDTO user = UserHolder.getUser();
        if(user==null)
            return Result.ok();
        Long userId = user.getId();
        if(isFollow)
        {
            followMapper.insertFollow(userId,id);
        }
        else{
            followMapper.deleteFollow(userId,id);
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long id) {
        UserDTO user = UserHolder.getUser();
        if(user==null)
            return Result.ok(false);
        Long userId = user.getId();
        int isFollow = followMapper.findIsFollow(userId, id);
        if(isFollow>0)
            return Result.ok(true);
        return Result.ok(false);
    }
}
